var  client = require('../database');
function  Tag(tag) {
    this.name =tag.name;
    this.fid =tag.fid;
}
var tableName = "tag";
mysql = client.getDbCon();
module.exports = Tag;

/** add tag */
Tag.prototype.save = function save(callback) {
    var  tag = {
        name: this.name,
        fid: this.fid,
    };
    var sql = "insert into tag (name,fid) values(?,?)";
    mysql.query(sql,[tag.name,tag.fid],function(err,results,fields){
        if (err) {
            throw err;
        } else {
            return callback(err, results[0], fields);
        }
    });
};

/** get tags*/
Tag.get = function get(callback){
    var sql = "select * from tag";
    mysql.query(sql,function(err,results,fields){
        if (err) {
            throw err;
        } else {
            return callback(err, results, fields);
        }
    });
}

/** get tag*/
Tag.getById = function getById(id,callback){
    var sql = "select * from tag where id = ?";
    mysql.query(sql,[id],function(err,result,fields){
        if (err) {
            throw err;
        } else {
            return callback(err, result[0], fields);
        }
    });
}